package com.heima.article.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @author shiminghui
 * @date 2024/11/8 22:40
 * @description: TODO
 */
@Slf4j
public class AutoImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        // 读取一个文件
        InputStream is = AutoImportSelector.class.getClassLoader().getResourceAsStream("auto-selector.text");
        if (is == null) {
            return new String[]{};
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        ArrayList<String> list = new ArrayList<>();
        try {
            String line = null;
            while ((line = reader.readLine()) != null && line.length() > 0) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("自动装配类加载成功");
        return list.toArray(new String[]{});
    }
}
